<?php
class Default_Model_UserGroup extends Zend_Db_Table{
	protected $_name 	= 'user_group';
	protected $_primary = 'id';
	
	
public function countItem($arrParam =null, $options = null){
	
		$db = Zend_Registry::get('connectDb');
		$ssFilter = $arrParam['ssFilter'];
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
					 ->from('user_group AS g',array('COUNT(g.id) AS totalItem'));
		if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('g.group_name LIKE ?',$keywords,'STRING');
			}
		$result = $db->fetchOne($select);
		//echo $result;
		return $result;
	}
	public function listItem($arrParam =null, $options = null){
		
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];		
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		if($options['task']== 'admin-list'){
			$select = $db->select()
						 ->from('user_group AS g',array('id','group_name','status','group_acp'))
						 ->joinLeft('users AS u','g.id = u.group_id','COUNT(u.id) AS members')
						 ->group('g.id');
			
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('g.group_name LIKE ?',$keywords,'STRING');
			}
			if(!empty($ssFilter['colum']) && !empty($ssFilter['by'])){
				$select->order($ssFilter['colum'].' '. $ssFilter['by']);
			}
			$row = $db->fetchAll($select);
		}
		return $row;
	}
	public function saveItem($arrParam=null,$options=null){
		if($options['task']== 'admin-add'){
			$row = $this->fetchNew();
			$row['group_name'] 		= $arrParam['group_name'];			
			$row['group_acp'] 		= $arrParam['group_acp'];			
			$row['status'] 			= $arrParam['status'];
			$row['created'] 		= date("Y-m-d H:s:i");
			
			/* $info =  new Zendvn_System_Info();
			$created_by = $info->getMemberInfo('id'); */
			$row['created_by'] 	= 1;
			$row->save();
		}
		if($options['task']== 'admin-edit'){			
			$where = 'id = '. $arrParam['id'];
			$row = $this->fetchRow($where);
			
			$row->group_name 		= $arrParam['group_name'];
			$row->group_acp 		= $arrParam['group_acp'];		
			$row->status 			= $arrParam['status'];
			
			/* $info =  new Zendvn_System_Info();
			$modified_by = $info->getMemberInfo('id'); */
			$row->modified_by 		= 0;
			$row->modified 			= date("Y-m-d H:s:i");
			$row->save();
		}
	}
	public function getItem($arrParam=null,$options=null){
		if($options['task']== 'admin-info'||$options['task']== 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter,$config);
			$select = $db->select()
						 ->from('user_group AS g')
						 ->where('id = ?',$arrParam['id']);
			$row = $db->fetchAll($select);
		}
		
		
		if($options['task']== 'delete'){
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
						 ->from('user_group AS g')
						 ->where('id = ?',$arrParam['id']);
			$row = $db->fetchAll($select);
		}
		return $row[0];
	}
	public function deleteItem($arrParam=null,$options=null){
		if($options['task']== 'admin-delete'){
			$where = 'id = '. $arrParam['id'];
			$this->delete($where);
		}		
		if($options['task']== 'admin-multidelete'){
			$cid = $arrParam['cid'];
			if(count($cid)>0){
				$ids = implode(',',$cid);
				$where = 'id IN ('.$ids.')';
				$this->delete($where);	
			}
		}
	}
	public function changeStatus($arrParam=null,$options=nul){
		
		if($options['task']== 'admin-status'){
			if (isset($arrParam['cid'])){	
				$cid = $arrParam['cid'];
				if(count($cid)>0){
					if($arrParam['type']==1){
						$status =1 ;
					}else{
						$status =0 ;
					}
					$ids = implode(',',$cid);
					$data = array('status'=>$status);
					$where = 'id IN ('.$ids.')';
					$this->update($data,$where);	
				}
			}
			if($arrParam['id']>0){
				if($arrParam['type'] ==1){
					$status =1 ;
				}else{
					$status =0 ;
				}
				$data = array('status'=>$status);
				$where = 'id ='. $arrParam['id'];
				$this->update($data,$where);
			}
		}
	
	}
	
}